package cn.youyeku.zsxh.Utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;


public class Tools {


    // 检查是否存在SDCard
    public static boolean hasSdcard(){
        String state = Environment.getExternalStorageState();
        if(state.equals(Environment.MEDIA_MOUNTED)){
            return true;
        }else{
            return false;
        }
    }


    public static  void writePreference(Context context, String xmlName, String key, Object object, int dataType) {

        SharedPreferences sharedPref = context.getSharedPreferences(xmlName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();

        switch (dataType) {
            case 2:
                editor.putString(key, object.toString());

                break;
            case 0:
                editor.putInt(key, (Integer) object);
                break;
            case 1:
                editor.putFloat(key, Float.parseFloat(object.toString()));
                break;
        }

        editor.apply();
    }

    public static Object getPreference(Context context, String xmlName, String key, int dataType) {
        SharedPreferences sharedPref = context.getSharedPreferences(xmlName, Context.MODE_PRIVATE);
        Object object = null;

        switch (dataType) {
            case 0:
                object = sharedPref.getInt(key, -1);
                break;
            case 1:
                object = sharedPref.getFloat(key, -1);
                break;
            case 2:
                object = sharedPref.getString(key, "");
                break;
        }
        return object;
    }

    public static void initEffect(Window window){
        //沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }
    }


    public static void getPhonePX(WindowManager windowManager){
        DisplayMetrics dm = new DisplayMetrics();
        // 获取屏幕信息
        windowManager.getDefaultDisplay().getMetrics(dm);
        int screenWidth = dm.widthPixels;
        int screenHeigh = dm.heightPixels;
        Log.v("获取屏幕宽度", "宽度:" + screenWidth + ",高度:" + screenHeigh);
    }

}
